package com.hy.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.common.Result;
import com.hy.dto.user.HeatMapData;
import com.hy.entity.UserAddress;
import com.hy.mapper.UserAddressMapper;
import com.hy.service.UserAddressService;
import com.hy.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements UserAddressService {

    @Autowired
    private UserUtil userUtil;
    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public List<UserAddress> getUserAddresses() {
        // 获取用户的id
        Integer weChatUserId = userUtil.getUserIdFromToken();

        // 查询用户的地址列表
        LambdaQueryWrapper<UserAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddress::getUserId, weChatUserId)
               .eq(UserAddress::getDeleteFlag, 0)
               .orderByDesc(UserAddress::getDefaultFlag)
               .orderByDesc(UserAddress::getUpdateTime);
        return list(wrapper);
    }

    @Override
    @Transactional
    public boolean setDefault(Long id) {

        // 先将该用户的所有地址设置为非默认
        LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserAddress::getUserId, userUtil.getUserIdFromToken())
                    .set(UserAddress::getDefaultFlag, 0);
        update(updateWrapper);

        // 将指定地址设置为默认
        UserAddress address = new UserAddress();
        address.setId(id);
        address.setDefaultFlag(1);
        return updateById(address);
    }

    @Override
    @Transactional
    public boolean delete(Long id) {
        LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(UserAddress::getId, id)
                    .eq(UserAddress::getUserId, userUtil.getUserIdFromToken())
                    .set(UserAddress::getDeleteFlag, 1);
        return update(updateWrapper);
    }

    /**
     * 保存地址
     */
    @Override
    public Result<Void> saveAddress(UserAddress address) {

        // 判断是否需要跟新默认
        if(ObjectUtil.isNotEmpty(address.getDefaultFlag()) && address.getDefaultFlag() == 1){
            // 先将该用户的所有地址设置为非默认
            LambdaUpdateWrapper<UserAddress> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(UserAddress::getUserId, userUtil.getUserIdFromToken())
                    .set(UserAddress::getDefaultFlag, 0);
            update(updateWrapper);
        }

        if(ObjectUtil.isEmpty(address.getId())){
            address.setUserId(userUtil.getUserIdFromToken());
            address.setDeleteFlag(0);
            address.setCreateTime(new Date());
            address.setUpdateTime(new Date());
            save(address);
        }else {
            address.setUpdateTime(new Date());
            updateById(address);
        }

        return Result.success();
    }

    /**
     * 获取省份级别的用户热力图数据
     */
    public List<HeatMapData> getProvinceHeatMapData() {
        return userAddressMapper.countUsersByProvince();
    }

    /**
     * 获取城市级别的用户热力图数据
     */
    public List<HeatMapData> getCityHeatMapData() {
        return userAddressMapper.countUsersByCity();
    }

    /**
     * 获取区县级别的用户热力图数据
     */
    public List<HeatMapData> getDistrictHeatMapData() {
        return userAddressMapper.countUsersByDistrict();
    }

    /**
     * 获取所有用户位置点数据
     */
    public List<HeatMapData> getAllUserLocations() {
        return userAddressMapper.getAllUserLocations();
    }

    // 获取省份用户分布
    public List<Map<String, Object>> getProvinceUserDistribution() {
        return userAddressMapper.countUsersByProvince2();
    }

    // 获取城市用户分布
    public List<Map<String, Object>> getCityUserDistribution(Long provinceId) {
        return userAddressMapper.countUsersByCity2(provinceId);
    }

    // 获取区县用户分布
    public List<Map<String, Object>> getDistrictUserDistribution(Long cityId) {
        return userAddressMapper.countUsersByDistrict2(cityId);
    }

    // 获取省份下的用户列表
    public List<String> getUsersByProvince(Long provinceId) {
        return userAddressMapper.listUsersByProvince(provinceId);
    }

    // 获取城市下的用户列表
    public List<String> getUsersByCity(Long cityId) {
        return userAddressMapper.listUsersByCity(cityId);
    }

    // 获取区县下的用户列表
    public List<String> getUsersByDistrict(Long districtId) {
        return userAddressMapper.listUsersByDistrict(districtId);
    }
}
